﻿Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient

Public Class cardAdmin
    Inherits System.Web.UI.Page

    'Empty Text Boxes on Page Load
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load, mustPlayDrpLstMod.Init, chooseCardDrpLst.Init


    End Sub

    Sub MySub()
        MsgBox("Hello")
    End Sub


    Sub addCard()
        'Connection Variables
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCommand As SqlCommand = New SqlCommand()
        Dim SqlCommandAttribute As SqlCommand = New SqlCommand()
        Dim SqlCommandEffect As SqlCommand = New SqlCommand()


        Dim idReturn As String
        SqlCommand.CommandText = "INSERT INTO Card (CardName, CardDesc, CardInstruction, CardCategoryID, MustPlay) " & _
                           "VALUES ( '" & cardNameTxt.Text.ToString & "' ," & _
                         " '" & cardDescriptionAddTxt.Text.ToString & "' , '" & cardInstructionsAddTxt.Text.ToString & "', '" & _
                         cardCategoryLstBx.SelectedValue & "', '" & mustPlayDrpLst.SelectedValue & "' )"
        'Checking which Attributes are selected
        
        'Returns the last inserted ID
        idReturn = connectLink.doUpdate2(SqlCommand.CommandText.ToString)
        SqlCommandAttribute.CommandText = "INSERT INTO Card_PlayAttribute (CardID, PlayAttributeID) VALUES " & _
            " ('" & idReturn & "', '" & playAttributeDDL.SelectedValue.ToString & "')"
        SqlCommandEffect.CommandText = "INSERT INTO Card_Effect (CardID, EffectID) VALUES ('" & idReturn & "','" & cardEffectDDL.SelectedValue.ToString & "')"

        If (connectLink.doUpdate(SqlCommand.CommandText.ToString) > -1 And connectLink.doUpdate(SqlCommandAttribute.CommandText.ToString) > -1 _
            And connectLink.doUpdate(SqlCommandEffect.CommandText.ToString) > -1) Then
            MsgBox("Card Has Been Added")
        End If
    End Sub
    'Save Modified Card
    Protected Sub saveModCard_Click(sender As Object, e As EventArgs) Handles saveModCard.Click
        'Connection Variables
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCommand As SqlCommand = New SqlCommand
        SqlCommand.CommandText = "UPDATE Card SET CardName = '" & cardNameModTxt.Text.ToString & "', CardDesc = '" & cardDescriptionModTxt.Text.ToString & _
            "', CardInstruction = '" & cardInstrModTxt.Text.ToString & "', CardCategoryID = '" & cardCategoriesModLstBx.SelectedValue & _
            "', MustPlay= '" & mustPlayModDrpLst.SelectedValue & "' WHERE CardID = '" & chooseCardDrpLst.SelectedValue.ToString & "'"
        If connectLink.doUpdate(SqlCommand.CommandText.ToString) > -1 Then
            MsgBox("Card Has Been Modified")
        End If
        clearOutForms()
    End Sub

    'Select Card from database and Fill all the Fields
    Public Sub chooseCardDrpLst_SelectedIndexChanged(sender As Object, e As EventArgs) Handles chooseCardDrpLst.SelectedIndexChanged
        'Connection Variables
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCommand As SqlCommand = New SqlCommand
        Dim SqlCommandConstructType As SqlCommand = New SqlCommand
        SqlCommand.CommandText = "SELECT * FROM CARD WHERE CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
        SqlCommandConstructType.CommandText = "SELECT * FROM Card_Construct WHERE CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
        'Create a SQL Command Variable
        Dim connReader As SqlDataReader
        'Run Query
        Try
            'Return Reader to fill out Card, Description and Instruction fields
            connReader = connectLink.getDataReader(SqlCommand.CommandText)
            'Read output for system box
            'Check if Data Reader has rows
            If connReader.HasRows Then
                'Read data from reader
                While connReader.Read()
                    cardNameModTxt.Text = connReader("CardName").ToString
                    cardDescriptionModTxt.Text = connReader("CardDesc").ToString
                    cardInstrModTxt.Text = connReader("CardInstruction").ToString
                    cardCategoriesModLstBx.SelectedIndex = cardCategoriesModLstBx.Items.IndexOf(cardCategoriesModLstBx.Items.FindByValue(connReader("CardCategoryID")))
                    mustPlayModDrpLst.SelectedIndex = mustPlayModDrpLst.Items.IndexOf(mustPlayModDrpLst.Items.FindByText(connReader("MustPlay")))
                End While
                connReader.Close()
            End If
            'Return Reader to select Construct Type
            connReader = connectLink.getDataReader(SqlCommandConstructType.CommandText)
            'Read output for system box
            'Check if Data Reader has rows
            If connReader.HasRows Then
                'Read data from reader
                While connReader.Read()
                    constructTypeModDDL.SelectedIndex = constructTypeModDDL.Items.IndexOf(constructTypeModDDL.Items.FindByText(connReader("ConstructID")))
                End While
            End If
            'Read all attributes
            connReader.Close()
        Catch ex As Exception
            connReader.Close()
        End Try
    End Sub

    'remove Card from the database. This function only removes card from Card Table
    Protected Sub removeCard_Click(sender As Object, e As EventArgs) Handles removeCard.Click
        Dim connectLink As DBConnect = New DBConnect()
        Dim SqlCommand0 As SqlCommand = New SqlCommand
        Dim SqlCommand1 As SqlCommand = New SqlCommand
        Dim SqlCommand2 As SqlCommand = New SqlCommand
        Dim SqlCommand3 As SqlCommand = New SqlCommand
        Dim SqlCommand4 As SqlCommand = New SqlCommand
        Dim SqlCommand5 As SqlCommand = New SqlCommand
        Dim SqlCommand6 As SqlCommand = New SqlCommand
        Dim SqlCommand7 As SqlCommand = New SqlCommand
        Dim SqlCommand8 As SqlCommand = New SqlCommand

        'Delete Items
        If (chooseCardDrpLst.SelectedValue.Length > 0) Then
            SqlCommand0.CommandText = "Delete from card where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand1.CommandText = "Delete from Card_PlayAttribute where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand2.CommandText = "Delete from CardInPlay_CardEffectSystemAttribute where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand3.CommandText = "Delete from CardEffect_SystemAttribute where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand4.CommandText = "Delete from Card_Effect where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand5.CommandText = "Delete from Card_Construct where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand6.CommandText = "Delete from CardPlayAttribute_SystemAttribute where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand7.CommandText = "Delete from MatchPlayer_Card where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"
            SqlCommand8.CommandText = "Delete from Match_GameCard where CardID='" & chooseCardDrpLst.SelectedValue.ToString & "'"

            If (connectLink.doUpdate(SqlCommand3.CommandText.ToString) > -1 Or connectLink.doUpdate(SqlCommand2.CommandText.ToString) > -1 _
                Or connectLink.doUpdate(SqlCommand4.CommandText.ToString) > -1 Or connectLink.doUpdate(SqlCommand6.CommandText.ToString) > -1 _
                Or connectLink.doUpdate(SqlCommand7.CommandText.ToString) > -1 Or connectLink.doUpdate(SqlCommand8.CommandText.ToString) > -1 _
                Or connectLink.doUpdate(SqlCommand5.CommandText.ToString) > -1 Or connectLink.doUpdate(SqlCommand1.CommandText.ToString) > -1 _
                Or connectLink.doUpdate(SqlCommand0.CommandText.ToString) > -1) Then
                MsgBox("Card Has Been Deleted")
            End If
        End If
        clearOutForms()
        chooseCardDrpLst.DataBind()

    End Sub

    'Refresh Forms
    Protected Sub clearOutForms()
        chooseCardDrpLst.SelectedIndex = chooseCardDrpLst.Items.IndexOf(chooseCardDrpLst.Items.FindByText("Select a Card..."))
        mustPlayDrpLst.SelectedIndex = mustPlayDrpLst.Items.IndexOf(mustPlayDrpLst.Items.FindByText("Choose Item"))
        cardNameModTxt.Text = ""
        cardNameTxt.Text = ""
        cardDescriptionModTxt.Text = ""
        cardDescriptionAddTxt.Text = ""
        cardInstrModTxt.Text = ""
        cardInstructionsAddTxt.Text = ""
    End Sub

    Protected Sub insertCard(sender As Object, e As EventArgs) Handles saveCard.Click
        addCard()
    End Sub
End Class